<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <p id="time">2020-12-01 16:50:00</p>
    <script>
        var endTime = "2020-12-12 16:50:00";
        var p = document.getElementById("time");
        
        function showTime(endTime){
            endTime = new Date(endTime).getTime();//讲获取的时间转化为毫秒
            // console.log('endTime: ', endTime);
            var nowTime = new Date().getTime();
            var gapTime = endTime - nowTime;//时间差值的毫秒数
            // console.log('gapTime: ', gapTime);
            var allSeconds = Math.floor(gapTime/1000);//取秒数
            var hours = Math.floor(allSeconds/3600);//取小时数
            var minutes = Math.floor(allSeconds%3600/60);//先取余，看剩下的时间的转为分钟有多少时间
            var seconds = Math.floor(allSeconds%60);//看剩下多少秒
            // console.log(allSeconds);
            // console.log(hours);
            // console.log(minutes);
            // console.log(seconds);
            var countdown = String(hours)+":"+String(minutes)+":"+String(seconds);
            // console.log(countdown);
            return countdown;
        }

        setInterval(function(){
            p.innerHTML = showTime(endTime);
        },1000)
    </script>
</body>

</html>